package org.fjsei.yewu.repository.maint;

import graphql.schema.DataFetchingEnvironment;
import jakarta.persistence.*;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.fjsei.yewu.filter.Uunode;
import org.fjsei.yewu.util.Tool;

import java.time.LocalDateTime;
import java.util.UUID;

import static jakarta.persistence.FetchType.LAZY;

//import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
//import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

/**
 * 事先数据库中须建立schema = "perf",
 * 进控制台CRDB窗口命令的：/root/ cockroach sql --certs-dir=certs --host=192.168.109.65:26257
 *后执行USE seipf; 接着DROP SCHEMA IF EXISTS perf CASCADE;  CREATE SCHEMA perf AUTHORIZATION user1;  GRANT USAGE ON SCHEMA perf TO user2;
*/

@Slf4j
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
//由于CockroachDB不支持catalog作为不同的命名空间
@Table(schema = "perf",indexes={@Index(columnList = "groupName, jobName",unique=true)} )
@Builder(toBuilder=true)
public class SplJob implements Uunode {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private UUID id;
    //为了 分片任务允许被别人抢去； CRDB?能用吗？
    //【问题】加version乐观锁实际对CRDB没意义了，CRDB小强数据库的内置事务隔离等级更高。IDEA的数据库工具会无意识添加事务很可能导致后端程序事务超慢或失败，@必须重启动IDEA。
    @Version
    @Column(name = "version")
    private int  version;
    private String groupName;

    private String jobName;    //作业标识代码；
    //分片的ID范围 [, ]
    private String start;
    //不能用end名字
    private String endr;
    /**当前质心位置的哪一行
     * */
    private String curr;
    private LocalDateTime last;   //最近运行
    private Long  offs;
    private int  limt;
    @Lob
    //    @Basic(fetch=LAZY)
    //    @Column( columnDefinition="jsonb")
    private String  result;

    //因为特例JsliceMang syncEqpFromLegacy ：允许ID=null;
    @Override
    public  String getId(DataFetchingEnvironment env){
        String typeName= this.getClass().getSimpleName();
        return (null==this.getId()? null : Tool.toGlobalId(typeName, this.getId()) );
    }

}
